package com.azkj.album.view.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.azkj.album.R;


public class CircleLinearLayout extends LinearLayout {
    private int left_topRadius, right_topRadius,
            left_downRadius,
            right_downRadius,
            cornerRadius,
            stroke_Width, solidColor, solidPressColor, stroke_Color;
    private GradientDrawable drawable;

    public CircleLinearLayout(Context context) {
        super(context);
        init();
    }

    public CircleLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleLinearLayout, defStyleAttr, 0);
        stroke_Color = a.getColor(R.styleable.CircleLinearLayout_stroke_Color, Color.TRANSPARENT);
        solidColor = a.getColor(R.styleable.CircleLinearLayout_solid_color, Color.TRANSPARENT);
        solidPressColor = a.getColor(R.styleable.CircleLinearLayout_solid_press_color, Color.WHITE);
        stroke_Width = (int) a.getDimension(R.styleable.CircleLinearLayout_circle_stroke_width, 0);
        cornerRadius = (int) a.getDimension(R.styleable.CircleLinearLayout_radius, 0);
        left_topRadius = (int) a.getDimension(R.styleable.CircleLinearLayout_left_topRadius, 0);
        right_topRadius = (int) a.getDimension(R.styleable.CircleLinearLayout_right_topRadius, 0);
        left_downRadius = (int) a.getDimension(R.styleable.CircleLinearLayout_left_downRadius, 0);
        right_downRadius = (int) a.getDimension(R.styleable.CircleLinearLayout_right_downRadius, 0);

        a.recycle();
        init();
    }

    private void init() {
        drawable = new GradientDrawable();
        drawable.setColor(solidColor);
        drawable.setStroke(stroke_Width, stroke_Color);
        if (cornerRadius != 0) {
            drawable.setCornerRadius(cornerRadius);
        } else {
            drawable.setCornerRadii(new float[]{left_topRadius, left_topRadius,
                    right_topRadius, right_topRadius, right_downRadius, right_downRadius, left_downRadius, left_downRadius,});
        }
        if (solidPressColor != Color.WHITE) {
            GradientDrawable pressDrawable = new GradientDrawable();
            pressDrawable.setColor(solidPressColor);
            pressDrawable.setStroke(stroke_Width, stroke_Color);
            if (cornerRadius != 0) {
                pressDrawable.setCornerRadius(cornerRadius);
            } else {
                pressDrawable.setCornerRadii(new float[]{left_topRadius, left_topRadius,
                        right_topRadius, right_topRadius, right_downRadius, right_downRadius, left_downRadius, left_downRadius});
            }

            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
            stateListDrawable.addState(new int[]{}, drawable);
            setBackground(stateListDrawable);
        } else {
            setBackground(drawable);
        }


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    public void setSolidColor(int color) {
        drawable.setColor(color);
        setBackground(drawable);
    }

    public void setStrokeColor(int color, int width) {
        drawable.setStroke(width, color);
        setBackground(drawable);
    }
}
